import type { UserConfig } from 'vite';

import vue from "@vitejs/plugin-vue";
import vueJsx from "@vitejs/plugin-vue-jsx";
import { resolve } from "path";
import { Alias } from "vite";

export default (): UserConfig => {
  const root = process.cwd();

  return {
    root,
    resolve: {
      alias: createAlias([
        // /@/xxxx => src/xxxx
        ['/@/', 'src'],
        // /#/xxxx => types/xxxx
        ['/#/', 'types'],
      ]),
    },

    build: {
      // minify: 'esbuild',
      outDir: 'dist',
      terserOptions: {
        compress: {
          keep_infinity: true,
          // Used to delete console in production environment
          drop_console: true,
        },
      },
      // Turning off brotliSize display can slightly reduce packaging time
      brotliSize: false,
      chunkSizeWarningLimit: 1200,
    },
    define: {
      // setting vue-i18-next
      // Suppress warning
      __VUE_I18N_LEGACY_API__: false,
      __VUE_I18N_FULL_INSTALL__: false,
      __INTLIFY_PROD_DEVTOOLS__: false,
    },

    // The vite plugin used by the project. The quantity is large, so it is separately extracted and managed
    plugins: [
      // have to
      vue(),
      // have to
      vueJsx(),
    ]
  };
};

function pathResolve(dir: string) {
  return resolve(__dirname, '.', dir);
}

export function createAlias(alias: [string, string][]): Alias[] {
  return alias.map((item) => {
    const [alia, src] = item;
    return {
      find: new RegExp(alia),
      replacement: pathResolve(src) + '/',
    };
  });
}
